package database;

import readfile.*;
import fingerprint.*;

import java.io.File;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class BuildTable {

    public Connect nc = new Connect();
    private Statement ent = null;
    private ResultSet res = null;
    private static InsertSF insert = new InsertSF();
    private static SearchSF search = new SearchSF();

    public static void main(String[] args) {
        buildSongTable("D:\\MusicTest");//请自行改为自己的歌曲文件夹目录,下同
        System.out.println("build song table successfully");
        buildFingerTable("D:\\MusicTest");
        System.out.println("insert successfully");
        CreateIndex.createIndex();
        System.out.println("create index successfully");

    }


    public static void buildSongTable(String musicFolderPath) {
        File folder = new File(musicFolderPath);
        if(!folder.isDirectory()) {
            throw new RuntimeException("it is not a music directory!");
        }
        String[] musics = folder.list();
        insert.Insertsong(musics);
    }

    public static void buildFingerTable(String musicFolderPath) {
        File folder = new File(musicFolderPath);
        if(!folder.isDirectory()) {
            throw new RuntimeException("it is not a music directory!");
        }
        String[] musics = folder.list();
        long startMillis = System.currentTimeMillis();
        final long initMillis = startMillis;
        long currentMillis;
        for (int i = 0; i < musics.length; i++) {
            System.out.println("正在为第"+String.valueOf(i+1)+"首歌生成指纹...");
            double[] data = GetDoubleArray.GetDoubles(musicFolderPath+"/"+musics[i]);
            FingerPrint fingerPrint = new FingerPrint(i);
            fingerPrint.generate(data);
            ArrayList<ShazamHash> list = fingerPrint.combineHash();
            insert.Insert(list);
            currentMillis = System.currentTimeMillis();
            System.out.println("第"+String.valueOf(i+1)+"首歌的指纹已存入,用时"+String.valueOf(currentMillis - startMillis)+"ms，总用时"+String.valueOf(currentMillis - initMillis)+"ms");
            startMillis = currentMillis;
        }
    }
}